package com.topscomm.pm.script;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.IPmCalendarService;
import com.topscomm.pm.service.IPmResourceChangeLineService;
import com.topscomm.pm.service.impl.PmCalendarService;
import com.topscomm.pm.service.impl.PmProjectChangeService;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pm.service.impl.PmResourceChangeLineService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;

import cn.hutool.core.date.DateUtil;

public class PmResourceChangeScript extends PmScript{
	/**
	 * 项目资源变更审批结束后回写资源明细信息
	 * @param service
	 * @param entity
	 * @throws Exception
	 */
	public void resourceChangeApproval(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		IPmResourceChangeLineService pmResourceChangeLineService = (PmResourceChangeLineService)SpringUtil.getBean("pmResourceChangeLineService");
		//查询单据具体信息
		List<Map<String, Object>> listPmresourceChangeLine = pmResourceChangeLineService.queryMapByWhere("resourcechangeid = '"+sourceid+"'");
		//更新资源申请明细表的相关字段
		List<Map<String, Object>> updateBackParam = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> pmProjectChange:listPmresourceChangeLine)
		{
			Map<String, Object> updateParam = new HashedMap<>();
			updateParam.put("id", ConvertUtil.convertToString(pmProjectChange.get("resourceapplylineid"))); // 资源申请明细
			updateParam.put("starttime",ConvertUtil.convertToString(pmProjectChange.get("starttime")));
			updateParam.put("endtime", ConvertUtil.convertToString(pmProjectChange.get("endtime")));
			updateParam.put("totamoney",  ConvertUtil.convertToString(pmProjectChange.get("totamoney")));
			updateParam.put("num", ConvertUtil.convertToString(pmProjectChange.get("num")));
			updateBackParam.add(updateParam);
		}
		// 批量更新资源明细
		pmResourceChangeLineService.updateBatchMap(updateBackParam);
	}
}
